#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<unordered_map>
#include<algorithm>
using namespace std;
class Solution5
{
public:
    bool isInterleave(string s1, string s2, string s3)
    {
        if ((s1.size() + s2.size()) < s3.size()) return false;
        s1 = " " + s1;s2 = " " + s2;s3 = " " + s3;
        int m = s1.size(), n = s2.size();
        vector<vector<bool>> dp(m, vector<bool>(n));
        dp[0][0] = true;
        for (int i = 1;i < m;i++)
            if (s1[i] == s3[i]) dp[i][0] = true;
            else break;
        for (int i = 1;i < n;i++)
            if (s2[i] == s3[i]) dp[0][i] = true;
            else break;
        for (int i = 1;i < m;i++)
            for (int j = 1;j < n;j++)
                if ((s1[i] == s3[i + j] && dp[i - 1][j])
                    || (s2[j] == s3[i + j] && dp[i][j - 1]))
                    dp[i][j] = true;
        return dp[m - 1][n - 1];
    }
};
class Solution
{
public:
    bool canPartition(vector<int>& nums)
    {
        int sum = 0, target = 0;
        for (auto& e : nums) sum += e; target = sum / 2;
        if ((target * 2) != sum) return false;
        int n = nums.size();
        vector<vector<bool>> dp(n + 1, vector<bool>(target + 1, true));
        for (int i = 1;i <= target;i++) dp[0][i] = false;
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= target;j++)
            {
                if (i == 4)
                {
                    int a = 0;
                }
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i-1]) dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i-1]];
            }
        return dp[n][target];
    }
};
int main()
{
    vector<int> v = { 1,5,11,5 };
    Solution().canPartition(v);
    /*string s1= "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
        s2= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        s3 = a;
    Solution().isInterleave()*/
	return 0;
}